/*****************************************************
 * Date: 2025-5-26
 * Time: 20:05
 * Author: Jonah
 * **************************************************/
#include "Key.h"

/****************************************************
 * Function: Scan Keys handle function
 * Usage:call it in while(1)
 * Parameter:
 *     mode = 1: support continuous press
 *     mode = 0: Do not support continuous press
 * Return Value: Key code(such as: KEY0_PRES)
 * Notice: Handle priority: KEY0>KEY1>KEY2>WK_UP!
 * *************************************************/
uint8_t KEY_Scan(uint8_t mode)
{
	/* Check the parameters */
	assert_param(IS_KEY_CONTINUOUS_MODE(mode));

    static uint8_t key_up = 1;     //按键松开标志
    if(mode == 1)
    {
    	key_up = 1;    //支持连按
    }
    // if(key_up && (KEY0 == 0 || KEY1 ==  0 || KEY2 == 0 || WK_UP == 1))
    if(key_up && (KEY0 == 0 || KEY1 ==  0 || KEY2 == 0 ))
    {
    	HAL_Delay(10);

        key_up = 0;

        if(0 == KEY0)
        	return KEY0_PRES;
        else if(KEY1==0)
        	return KEY1_PRES;
        else if(KEY2==0)
        	return KEY2_PRES;
        // else if(WK_UP==1)
        // 	return WKUP_PRES;
        else
        {
            // do nothing
        }
    }
    // else if(KEY0 == 1 && KEY1 == 1 && KEY2 == 1 && WK_UP == 0)
    else if(KEY0 == 1 && KEY1 == 1 && KEY2 == 1)
    {
    	key_up = 1;
    }
    else
    {
    	/*do nothing*/
    }
    return 0;   //无按键按下
}


